Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

[ 紀錄 ] 實戰練習 - Todo List ( 以 JS 實作前端 + PHP 後端 )

[ 紀錄 ] 實戰練習 - Todo List ( 以 JS 實作前端 + PHP 後端 )

Computer Security

Computer Security

AI輔導室|馬賽克拼貼效果

AI輔導室|馬賽克拼貼效果






留言討論